package com.lalosoft.misgastos.view;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lalosoft.misgastos.R;
import com.lalosoft.misgastos.activities.ItemListActivity;
import com.lalosoft.misgastos.domain.Item;
import com.lalosoft.misgastos.listener.DeleteItemListener;

public class ItemListItemView extends LinearLayout{
	
	private TextView detalle;
	private TextView valorCuota;
	private TextView cantidadCuotas;
	private TextView fechaCompra;
	private TextView cuotasRestantes;
	private RelativeLayout relative;
	private DeleteItemListener listener;
	
	private Item item;
	
	public ItemListItemView(Context context) {
		super(context);
		init(context);
	}
	
	public ItemListItemView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}
	
	private void init(final Context context){
		inflate(context, R.layout.list_item, this);
		detalle = (TextView) findViewById(R.id.list_item_detalle);
		valorCuota = (TextView) findViewById(R.id.list_item_valor_cuota);
		cantidadCuotas = (TextView) findViewById(R.id.list_item_cantidad);
		fechaCompra = (TextView) findViewById(R.id.list_item_fecha_compra);
		cuotasRestantes = (TextView) findViewById(R.id.list_item_restantes);
		relative = (RelativeLayout) findViewById(R.id.list_item_relative);
		relative.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(final View v) {
				Toast.makeText(context, "Mostrar item", Toast.LENGTH_SHORT).show();
				final AlertDialog.Builder builder = new Builder(context);
				String[] names = {"Ver mas", "Borrar"};
				builder.setTitle(item.getDetalle());
				builder.setItems(names, new DialogInterface.OnClickListener() {
            		 
	         			@Override
						public void onClick(DialogInterface dialog, int which) {
	         				switch(which){
	         				case 0:{
	         					Toast.makeText(context, "Ver mas", Toast.LENGTH_SHORT).show();
	         				}break;
	         				case 1:{
	         					listener.deleteItem(item);
	         					Intent i = new Intent(context, ItemListActivity.class);
	         					v.getContext().startActivity(i);
	         					Toast.makeText(context, "Se ha borrado el item!", Toast.LENGTH_SHORT).show();
	         				}break;
	         				}
						}
	            	 });
				 builder.show();
			}
		});
	}
	
	public void setItem(Item item){
		detalle.setText(item.getDetalle());
		valorCuota.setText("Valor cuota: " + String.valueOf(item.getValorCuota()));
		cantidadCuotas.setText(" Total: " + String.valueOf(item.getCantidadCuotas()));
		fechaCompra.setText(item.getFechaCompra());
		cuotasRestantes.setText("Restan: " + String.valueOf(item.getCuotasRestantes()));
		this.item = item;
	}

	public void setActionItemListener(DeleteItemListener listener) {
		this.listener = listener;
	}
}
